<?php
include_once ('class.mysql.php');

class EditMachine
{
    private $comtec_id_machine;
    private $comtec_id_responsible;
    private $tag_id_machine;
    private $brand;
    private $cod_mainteiner;
    private $model;
    private $year;
    private $comtec_id_owner;
    private $comtec_id_configman;
    private $cod_owner;
    private $notes;
    private $date_next_maintenance;
    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
        $this->comtec_id_responsible = $_POST['comtec_id_responsible'];
        $this->comtec_id_owner = $_POST['comtec_id_owner'];
        $this->comtec_id_machine = $_POST['comtec_id_machine'];
        $this->tag_id_machine = $_POST['tag_id_machine'];
        $this->brand = $_POST['brand'];
        $this->cod_mainteiner = $_POST['cod_mainteiner'];
        $this->cod_owner = $_POST['cod_owner'];
        $this->model = $_POST['model'];
        $this->year = $_POST['year'];
        $this->comtec_id_configman = $_POST['comtec_id_configman'];
        $this->notes = $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->date_next_maintenance = $_POST['date_next_maintenance'];
        
        $this->token    = $_POST['token'];
    }
    
    public function process()
    {
        if($this->valid_token() && $this->valid_data())
            $this->edit();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function edit()
    {
        $db = new MySQL();
        
        $tsql  = "UPDATE machines SET ";
	$tsql .= "comtec_id_owner= $this->comtec_id_owner , ";
        $tsql .= "tag_id_machine= '$this->tag_id_machine' , ";
        $tsql .= "brand= '$this->brand' , ";
        $tsql .= "cod_mainteiner= '$this->cod_mainteiner' , ";
        $tsql .= "model= '$this->model' , ";
        $tsql .= "year= $this->year , ";                             
        $tsql .= "comtec_id_configman= $this->comtec_id_configman , ";
        $tsql .= "cod_owner= '$this->cod_owner' , ";
        $tsql .= "notes= '$this->notes' , "; 
        $tsql .= "date_next_maintenance= '$this->date_next_maintenance' , ";
        $tsql .= "comtec_id_responsible= $this->comtec_id_responsible ";
	$tsql .= " WHERE comtec_id_machine= $this->comtec_id_machine ";
         
        $result = $db->query($tsql);

        if($db->affected_rows() < 1)
            $this->errors[] = 'No se puede procesar la edici&oacute;n';
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data()
    {
        //if($this->user_exists())
          //  $this->errors[] = 'Direccion de correo electronico ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
